package com.dayu.tcp.common;

import io.netty.channel.epoll.Epoll;

/**
 * @author yehangtech
 * @date 2024/4/2
 * @description 判断运行环境的机器平台
 */
public final class Platform {
    private Platform() {
    }

    public static final int UNSPECIFIED = -1;
    public static final int MAC = 0;
    public static final int LINUX = 1;
    public static final int WINDOWS = 2;
    public static final int WINDOWS_CE = 6;
    public static final int ANDROID = 8;
    private static final int OS_TYPE;

    static {
        String osName = System.getProperty("os.name");
        if (osName.startsWith("Linux")) {
            if ("dalvik".equalsIgnoreCase(System.getProperty("java.vm.name"))) {
                OS_TYPE = ANDROID;
            } else {
                OS_TYPE = LINUX;
            }
        } else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
            OS_TYPE = MAC;
        } else if (osName.startsWith("Windows CE")) {
            OS_TYPE = WINDOWS_CE;
        } else if (osName.startsWith("Windows")) {
            OS_TYPE = WINDOWS;
        } else {
            OS_TYPE = UNSPECIFIED;
        }
    }

    public static int getOSType() {
        return OS_TYPE;
    }

    public static boolean isMac() {
        return OS_TYPE == MAC;
    }

    public static boolean isAndroid() {
        return OS_TYPE == ANDROID;
    }

    public static boolean isLinux() {
        return OS_TYPE == LINUX;
    }

    public static boolean isWindowsCE() {
        return OS_TYPE == WINDOWS_CE;
    }

    public static boolean isWindows() {
        return OS_TYPE == WINDOWS || OS_TYPE == WINDOWS_CE;
    }

    public static boolean useEpoll() {
        return isLinux() && Epoll.isAvailable();
    }
}
